package com.xunshibao.dao.info;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xunshibao.core.dao.impl.BaseDaoImpl;
import com.xunshibao.core.dao.page.Pagination;
import com.xunshibao.entity.info.TutorInfo;

public class TutorDaoImpl extends BaseDaoImpl {
	private static final String GET_BY_ID = "select t from TutorInfo t where t.id=:id";
	private static final String GET_BY_USER_AND_STATUS = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t where t.poster=:userId and t.status=:status order by t.id desc";
	private static final String GET_BY_USER = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t where  t.status!=:status and t.poster=:userId order by t.id desc";
	private static final String ALL_BYSTATUS = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t where t.status=:status order by t.id desc";
	private static final String ALL = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t order by t.id desc";
	private static final String ALL_BY_SUBJECT_STATUS = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t where t.subject=:subject and t.status=:status order by t.id desc";
	private static final String ALL_BY_SUBJECT = "select new(t.id, t.poster, t.postDate, t.title) from TutorInfo t where t.subject=:subject order by t.id desc";

	public TutorInfo getById(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		Object result = this.findSingle(GET_BY_ID, params);
		if (result != null) {
			return (TutorInfo) result;
		}
		return null;
	}

	public Pagination getByUserIdAndStatus(Long userId, TutorInfo.Status status, int pageNo, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("status", status.value());
		return this.pageFind(GET_BY_USER_AND_STATUS, pageNo, pageSize, params);
	}

	public Pagination getByUserId(Long userId, int pageNo, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("status", TutorInfo.Status.DELETED.value());
		return this.pageFind(GET_BY_USER, pageNo, pageSize, params);
	}

	public Pagination listAllBySubject(Integer subject, int pageNo, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("subject", subject);
		return this.pageFind(ALL_BY_SUBJECT, pageNo, pageSize, params);
	}

	public Pagination listAllBySubjectStatus(Integer subject, TutorInfo.Status status, int pageNo, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("subject", subject);
		params.put("status", status.value());
		return this.pageFind(ALL_BY_SUBJECT_STATUS, pageNo, pageSize, params);
	}

	public Pagination listAll(int pageNo, int pageSize) {
		return this.pageFind(ALL, pageNo, pageSize);
	}
	

	public Pagination listAllByStatus(TutorInfo.Status status, int pageNo, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", status.value());
		return this.pageFind(ALL_BYSTATUS, pageNo, pageSize, params);
	}
	
	public int updateStatus(List<Long> ids, TutorInfo.Status status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		String hql = "update TutorInfo t set t.status=:status where t.ids in (:ids)";
		return this.hqlUpdate(hql, params);
	}
	
	public int updateStatus(List<Long> ids, TutorInfo.Status status, Long userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		params.put("poster", userId);
		String hql = "update TutorInfo t set t.status=:status where t.ids in (:ids) and t.poster=:poster";
		return this.hqlUpdate(hql, params);
	}
}
